package com.zznj.xzjc.activity.index.zhishiku;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.view.RxToast;
import com.zznj.xzjc.R;
import com.zznj.xzjc.activity.SelectAreaActivity;
import com.zznj.xzjc.activity.index.push.IndexPushActivity;
import com.zznj.xzjc.base.BaseActivity;
import com.zznj.xzjc.net.SharedHelper;
import com.zznj.xzjc.net.entity.LoginBean;

import androidx.annotation.Nullable;
import butterknife.BindView;
import butterknife.OnClick;


/**
 * Created by JY on 2017/11/15.
 */

public class IndexZhiShiKuActivity extends BaseActivity {

    @BindView(R.id.searchEdit)
    EditText searchEdit;
    @BindView(R.id.areaTv)
    TextView areaTv;
    @BindView(R.id.searchBtn)
    View searchBtn;

    @BindView(R.id.btn_zfzt)
    View btn_xzzfaj;
    @BindView(R.id.btn_qlqd)
    View btn_tyywrj;
    @BindView(R.id.btn_alk)
    View btn_xspjaj;
    @BindView(R.id.btn_flfg)
    View btn_hbdc;

    @BindView(R.id.btn_kjwx)
    View btn_szrx;
    @BindView(R.id.btn_zjk)
    View btn_tsjb;
    @BindView(R.id.btn_jdjgk)
    View btn_jdjgk;


    @Override
    protected int getLayoutId() {
        return R.layout.gyss_activity_index_zhishiku_layout;
    }

    private LoginBean user;
    @Override
    public void initView() {
        setTitle("知识库");
        btnRight.setVisibility(View.VISIBLE);
        btnRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RxActivityTool.skipActivity(mContext, IndexPushActivity.class);
            }
        });
        user = new Gson().fromJson(SharedHelper.readValueByKeyForString(mContext, "Member"), LoginBean.class);



        /***
         * 监听输入框是否点击了搜索
         */
        searchEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (i == EditorInfo.IME_ACTION_SEARCH) {
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);//关闭软键盘
                    String search = searchEdit.getText().toString().trim();
                    Bundle bundle = new Bundle();
                    bundle.putString("searchCode",searchCode);
                    bundle.putString("areaCode",areaCode);
                    bundle.putString("areaName",areaName);
                    bundle.putString("districtLevel",districtLevel);
                    bundle.putString("selfLevel",selfLevel);
                    bundle.putInt("index", 0);
                    bundle.putString("key", searchEdit.getText().toString());
                    RxActivityTool.skipActivity(activity, ActivityZhiShiKuMain.class, bundle);
                    return true;
                }
                return false;
            }
        });
    }

    @Override
    public void initData() {

    }

    @OnClick({R.id.areaTv,R.id.searchBtn,R.id.btn_zfzt,R.id.btn_qlqd,R.id.btn_alk,R.id.btn_flfg,R.id.btn_zjk,R.id.btn_kjwx,R.id.btn_jdjgk,})
    public void onClick(View v) {
        if (R.id.areaTv == v.getId()) {

        } else if (R.id.searchBtn == v.getId()) {
            Bundle bundle = new Bundle();
            bundle.putString("searchCode",searchCode);
            bundle.putString("areaCode",areaCode);
            bundle.putString("areaName",areaName);
            bundle.putString("districtLevel",districtLevel);
            bundle.putString("selfLevel",selfLevel);
            bundle.putInt("index", 0);
            bundle.putString("key", searchEdit.getText().toString());
            RxActivityTool.skipActivity(activity, ActivityZhiShiKuMain.class, bundle);
        } else {
            if (R.id.btn_zfzt == v.getId()) {
                index = 1;
            } else if (R.id.btn_qlqd == v.getId()) {
                index = 2;
            } else if (R.id.btn_alk == v.getId()) {
                index = 3;
            } else if (R.id.btn_flfg == v.getId()) {
                index = 4;
            } else if (R.id.btn_zjk == v.getId()) {
                index = 6;
            } else if (R.id.btn_kjwx == v.getId()) {
                index = 5;
            }else if (R.id.btn_jdjgk == v.getId()) {
                index = 7;
            }
            Bundle bundle = new Bundle();
            bundle.putInt("index", index);
            RxActivityTool.skipActivity(activity, ActivityZhiShiKuMain.class, bundle);
        }

    }

    private String areaCode;
    private String searchCode;
    private String areaName;
    private String upCode;
    private String selfLevel;
    private String districtLevel;
    private int index = 0;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == 0) {
                if (data != null) {
                    areaCode = data.getStringExtra("areaCode");
                    areaName = data.getStringExtra("areaName");
                    upCode = data.getStringExtra("upCode");
                    selfLevel = data.getStringExtra("selfLevel");
                    districtLevel = data.getStringExtra("districtLevel");
                    if("3".equals(districtLevel)){
                        searchCode = upCode;
                    }else{
                        searchCode = areaCode;
                    }
                    areaTv.setText(areaName);
                }
            }
        }
    }
}
